package sk.funix.userstory.dao.hibernate;


import java.util.List;

import org.springframework.stereotype.Repository;

import sk.funix.userstory.dao.StoryDAO;
import sk.funix.userstory.domain.Story;
import sk.funix.userstory.domain.User;

/**		
 * @author Nicolas Milliard
 *
 */
@Repository
public class StoryHibernateDAO extends GenericHibernateDAO<Story, Long> implements
		StoryDAO {
	
	
	public StoryHibernateDAO(){
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Story> findEntriesStoriesByUser(User user, int firstResult, int maxResults){
    	return getEntityManager().createQuery("from story s where s.user.id = :id")
    		.setParameter("id", user.getId())
    		.setFirstResult(firstResult)
    		.setMaxResults(maxResults)
    		.getResultList();
    }
	
	@SuppressWarnings("unchecked")
	public List<Story> findAllStoriesByUser(User user){
    	return getEntityManager().createQuery("from story s where s.user.id = :id")
    		.setParameter("id", user.getId())
    		.getResultList();
    }
	
    public long countAllByUser(User user){
    	return (Long) getEntityManager().createQuery("select count(*) " +
    								"from story s " +
    								"where s.user.id = :id")
    							.setParameter("", user.getId())
    							.getSingleResult();
    }
	
}
